package com.laiketui.gateway.plugins.lang;

import com.laiketui.root.gateway.util.LangUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LanguageInterceptor implements HandlerInterceptor {

    private final static Logger logger = LoggerFactory.getLogger(LanguageInterceptor.class);

    //
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器时");
        String language = WebUtils.findParameterValue(request, "language");
        //前端传过来的语言
        LangUtils.setLanguage(StringUtils.isNotEmpty(language) ? language : "zh");
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }


}
